<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>不同年龄段用户设备支付行为差异</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.8/dist/chart.umd.min.js"></script>
    <style>
        .chart-container {
            max-width: 1000px;
            margin: 2rem auto;
            padding: 1.5rem;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
    </style>
</head>
<body class="bg-gray-50">
<div class="chart-container">
    <h2 class="text-xl font-bold mb-4">不同年龄段用户在不同设备上的支付转化率</h2>
    <div class="h-96">
        <canvas id="ageDevicePaymentChart"></canvas>
    </div>
</div>

<script>
    // 原始数据：不同年龄段在不同设备上的支付转化率
    const ageDeviceData = [
        { age: "1-20", device: "其他设备", rate: 0.0625 },
        { age: "1-20", device: "桌面端", rate: 0.09163802978235967 },
        { age: "1-20", device: "移动端", rate: 0.08485415691779753 },
        { age: "21-30", device: "其他设备", rate: 0.07142857142857142 },
        { age: "21-30", device: "桌面端", rate: 0.07717003567181926 },
        { age: "21-30", device: "移动端", rate: 0.07387241272783442 },
        { age: "31-40", device: "其他设备", rate: 0.041666666666666664 },
        { age: "31-40", device: "桌面端", rate: 0.060721340258980176 },
        { age: "31-40", device: "移动端", rate: 0.06261022927689594 },
        { age: "41-50", device: "其他设备", rate: 0.0 },
        { age: "41-50", device: "桌面端", rate: 0.048 },
        { age: "41-50", device: "移动端", rate: 0.04784030287385992 },
        { age: "51+", device: "桌面端", rate: 0.04580152671755725 },
        { age: "51+", device: "移动端", rate: 0.03 }
    ];

    // 转换为百分比格式
    const toPercent = (value) => (value * 100).toFixed(2);

    // 提取唯一的年龄段和设备类型
    const ageGroups = [...new Set(ageDeviceData.map(item => item.age))];
    const devices = ["桌面端", "移动端", "其他设备"];

    // 组织图表数据
    const datasets = devices.map(device => {
        const colors = {
            "桌面端": "#10B981",
            "移动端": "#F59E0B",
            "其他设备": "#6B7280"
        };

        return {
            label: device,
            data: ageGroups.map(age => {
                const item = ageDeviceData.find(d => d.age === age && d.device === device);
                return item ? toPercent(item.rate) : 0;
            }),
            backgroundColor: colors[device],
            borderColor: colors[device],
            borderRadius: 4,
            barPercentage: 0.6
        };
    });

    document.addEventListener("DOMContentLoaded", () => {
        new Chart(document.getElementById("ageDevicePaymentChart"), {
            type: "bar",
            data: {
                labels: ageGroups,
                datasets: datasets
            },
            options: {
                responsive: true,
                scales: {
                    x: {
                        title: { display: true, text: "年龄段" },
                        grid: { display: false }
                    },
                    y: {
                        beginAtZero: true,
                        max: 10,
                        title: { display: true, text: "支付转化率 (%)" },
                        ticks: { callback: v => v + "%" }
                    }
                },
                plugins: {
                    legend: { position: "top" },
                    tooltip: {
                        callbacks: { label: l => `${l.dataset.label}: ${l.raw}%` }
                    }
                },
                animation: { duration: 2000, easing: "easeOutQuart" }
            }
        });
    });
</script>
</body>
</html>